/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servidorped;

import Persistence.ConversationFactory;
import Persistence.UserFactory;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Micosil
 */
public class ServidorPED {

    public static void main(String[] args) throws IOException {
        //Inicialización del logger.       
        FileHandler filelog;        
        
        try {
            new File("./serverlog0.txt").createNewFile();
            new File("./serverlog1.txt").createNewFile();
            filelog = new FileHandler("./serverlog%g.txt", 5000, 2, true);
        } catch (IOException ex) {
            System.out.println("Log system startup failure - could not open the logfiles.");
            return;
        } catch (SecurityException ex) {
            System.out.println("Something, somewhere, is not working as intended. I suggest checking the file permissions.");
            return;
        }        
        Logger.getGlobal().addHandler(filelog);        
        //Si la persistencia va a dar errores, que los de ya.
        
        Persistence.UserPersistence userPerm = UserFactory.getUserPersistence(); 
        Persistence.ConversationPersistence convoPerm = ConversationFactory.getConversationPersistence();

        //Programa en sí
        ServerSocket connectionListener;        
        try {
            connectionListener = new ServerSocket(9000);
        } catch (IOException ex) {
            Logger.getGlobal().log(Level.SEVERE, ex.getMessage(), ex);
            return;
        }
        final boolean ever = true;
        connectionListener.setSoTimeout(1000);
        for (;ever;) {
            try{
            new Thread(new Conexion(connectionListener.accept())).start();
            }catch(SocketTimeoutException ex){
                Logger.getGlobal().log(Level.FINEST, null, ex);
            }
        }
    }
}
